package com.jinandi.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.jinandi.app.domain.User;
import com.jinandi.app.service.RoleService;

@Controller
@RequestMapping("/admin")
public class UserController extends BaseController {

	private RoleService roleService;

	@Autowired
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * <修改用户权限>
	 * @param uid
	 * @param webRequest
	 * @param model
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-8 下午1:48:41
	 */
	@RequestMapping(value = { "/user/{uid}/modifyRole" }, method = {RequestMethod.POST }, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String modifyRole(@PathVariable Long uid ,WebRequest webRequest, Model model) {
		try {
			String[] ids = webRequest.getParameterValues("ids");
			roleService.modifyRole(uid,ids);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}

	/**
	 * <查看用户权限>
	 * 
	 * @param id
	 * @param request
	 * @param modelMap
	 * @author CGC 创建日期:2013-5-7 下午4:46:22
	 */
	@RequestMapping(value = "/user/{id}/role.json", method = RequestMethod.POST)
	public void getRoleByUser(@PathVariable long id, WebRequest request,
			ModelMap modelMap) {
		List<Map<String, Object>> list = roleService.findList(id);
		modelMap.addAttribute("Result", "OK");
		modelMap.addAttribute("TotalRecordCount", list.size());
		modelMap.addAttribute("Records", list);
	}

	/**
	 * 
	 * <用户列表json>
	 * 
	 * @param request
	 * @param modelMap
	 * @author CGC 创建日期:2013-5-7 下午4:46:40
	 */
	@RequestMapping(value = "/user/user.json", method = RequestMethod.POST)
	public void getUserjson(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		Map<String,Object> params = new HashMap<String,Object>();
		if(map.containsKey("username")){
			params.put("username", map.get("username"));
		}
		if(map.containsKey("name")){
			params.put("name", map.get("name"));
		}
		Page<User> findUser = getUserService().findUser(params,map);
		getFormatJsonForJtable(modelMap, findUser);
	}

	/**
	 * <删除用户>
	 * 
	 * @param request
	 * @param modelMap
	 * @author CGC 创建日期:2013-5-7 下午4:47:01
	 */
	@RequestMapping(value = "/user/deluser.json")
	public void delUser(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		try {
			getUserService().deleteUserById(Long.valueOf(map.get("id")));
			modelMap.addAttribute("Result", "OK");
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.addAttribute("Result", "ERROR");
			modelMap.addAttribute("Message", e.getMessage());
		}
	}

	/**
	 * <修改用户>
	 * 
	 * @param request
	 * @param modelMap
	 * @author CGC 创建日期:2013-5-7 下午4:47:19
	 */
	@RequestMapping(value = "/user/modifyuser.json", method = RequestMethod.POST)
	public void modifyUser(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		try {
			User user = getUserService().findUserById(
					Long.valueOf(map.get("id")));
			user.setName(map.get("name"));
			user.setName(map.get("username"));
			user.setEnabled(Boolean.valueOf(map.get("enabled")));
			getUserService().saveUser(user);
			modelMap.addAttribute("Result", "OK");
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.addAttribute("Result", "ERROR");
			modelMap.addAttribute("Message", e.getMessage());
		}
	}

	/**
	 * <创建用户>
	 * 
	 * @param request
	 * @param modelMap
	 * @author CGC 创建日期:2013-5-7 下午4:47:33
	 */
	@RequestMapping(value = "/user/createuser.json", method = RequestMethod.POST)
	public void createuser(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		try {
			Md5PasswordEncoder encoder = new Md5PasswordEncoder();
			User user = new User();
			user.setName(map.get("name"));
			user.setUsername(map.get("username"));
			user.setPassword(encoder.encodePassword(map.get("password"),
					map.get("username")));
			if (map.containsKey("enabled")) {
				user.setEnabled(Boolean.valueOf(map.get("enabled")));
			}
			getUserService().saveUser(user);
			modelMap.addAttribute("Result", "OK");
			modelMap.addAttribute("Record", user);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.addAttribute("Result", "ERROR");
			modelMap.addAttribute("Message", e.getMessage());
		}
	}

}
